package com.rbac.interfaces.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.rbac.application.service.RoleService;
import com.rbac.common.annotation.OperationLog;
import com.rbac.common.result.Result;
import com.rbac.domain.dto.query.RolePageQuery;
import com.rbac.domain.dto.request.RoleCreateRequest;
import com.rbac.domain.dto.request.RoleUpdateRequest;
import com.rbac.domain.entity.Role;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "角色管理", description = "角色管理相关接口")
@RestController
@RequestMapping("/system/role")
@RequiredArgsConstructor
public class RoleController {

    private final RoleService roleService;

    @Operation(summary = "分页查询角色")
    @GetMapping("/page")
    @SaCheckPermission("system:role:query")
    public Result<IPage<Role>> pageRoles(RolePageQuery query) {
        return Result.success(roleService.pageRoles(query));
    }

    @Operation(summary = "获取所有启用的角色")
    @GetMapping("/enabled")
    @SaCheckPermission("system:role:query")
    public Result<List<Role>> getAllEnabledRoles() {
        return Result.success(roleService.getAllEnabledRoles());
    }

    @Operation(summary = "获取角色详情")
    @GetMapping("/{id}")
    @SaCheckPermission("system:role:query")
    public Result<Role> getRoleById(@PathVariable Long id) {
        return Result.success(roleService.getRoleWithPermissions(id));
    }

    @Operation(summary = "创建角色")
    @PostMapping
    @OperationLog("创建角色")
    @SaCheckPermission("system:role:add")
    public Result<Role> createRole(@Validated @RequestBody RoleCreateRequest request) {
        return Result.success("角色创建成功", roleService.createRole(request));
    }

    @Operation(summary = "更新角色")
    @PutMapping
    @OperationLog("更新角色")
    @SaCheckPermission("system:role:edit")
    public Result<Role> updateRole(@Validated @RequestBody RoleUpdateRequest request) {
        return Result.success("角色更新成功", roleService.updateRole(request));
    }

    @Operation(summary = "删除角色")
    @DeleteMapping("/{id}")
    @OperationLog("删除角色")
    @SaCheckPermission("system:role:delete")
    public Result<Void> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success("角色删除成功");
    }

    @Operation(summary = "批量删除角色")
    @DeleteMapping("/batch")
    @OperationLog("批量删除角色")
    @SaCheckPermission("system:role:delete")
    public Result<Void> deleteBatchRoles(@RequestBody List<Long> ids) {
        roleService.deleteBatchRoles(ids);
        return Result.success("角色批量删除成功");
    }

    @Operation(summary = "分配权限")
    @PostMapping("/{id}/permissions")
    @OperationLog("分配权限")
    @SaCheckPermission("system:role:edit")
    public Result<Void> assignPermissions(@PathVariable Long id, @RequestBody List<Long> permissionIds) {
        roleService.assignPermissions(id, permissionIds);
        return Result.success("权限分配成功");
    }
}